package edu.yang.dbutils.result;

import edu.yang.dbutils.RowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author yang
 * @date 2024-05-12
 */
public class ColumnListHandler<T> extends AbstractListHandler<T>{
    private Integer columnIndex;
    private String columnName;

    public ColumnListHandler(String columnName) {
        this.columnName = columnName;
    }

    public ColumnListHandler(Integer columnIndex) {
        this.columnIndex = columnIndex;
    }

    /**
     * 根据列名或下标查询数据
     * @param rs
     * @return
     * @throws SQLException
     */
    @Override
    public T handleRow(ResultSet rs) throws SQLException {
        if (columnIndex != null){
            return (T) RowProcessor.toValue(rs, columnIndex);
        }
        return (T) RowProcessor.toValue(rs, columnName);
    }
}
